##This file produces Tables 1 from Loeffler (2014), "Detecting Gunshots Using Wearable Accelerometers"


require(glmnet)
training_all<-read.csv("C:/Users/cloef/Box Sync/Gunshot Detection/Data/training_sample.csv")
test<-read.csv("C:/Users/cloef/Box Sync/Gunshot Detection/Data/test_sample.csv")

y<-as.matrix(training_all[28])
x<-as.matrix(training_all[c(1:27,29:30)])
set.seed(2)
cvfit = cv.glmnet(x, y, family = "binomial",alpha = 1, type.measure = "class")
print(cvfit)

##Final Model
coef(cvfit, s = "lambda.min")

##Table 1
ytest<-as.matrix(test[28])
xtest<-as.matrix(test[c(1:27,29:30)])
show(table(actual=ytest,predicted=predict(cvfit,newx=xtest,type="class",s="lambda.min")))
